package Test1;

public class ThreadTest {

    public static void main(String[] args) throws InterruptedException {

        //two methods to get the answer.
           method01();
           method02();

    }

    public static void method01() throws InterruptedException {
        System.out.println("==========method 1============");
        Client c1 = new Client("Client1");
        Client c2 = new Client("Client2");
        Service s = new Service();
        s.start();
        c1.start();
        Thread.sleep(100);
        c2.start();
        c2.join();
        System.out.println("Simulation ends");
    }

    public static void method02()throws InterruptedException{
        String object = "1";
        System.out.println("==========method 2============");
       Client1 s = new Client1();

        Thread t1 = new Thread(()-> {
            int number=1;
            Client1 c1 = new Client1("Client1", number);
            while(number<=10) {
                synchronized (object) {
                    object.notifyAll();
                    c1.setNumber(number);
                    number++;
                    try {
                        c1.run();//打印
                        if(number<=10) {
                            object.wait();
                        }

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }

        });


        Thread t3 = new Thread(()->{
            int number=1;
            Client1 c2 = new Client1("Client2", number);
            while(number<=10) {
                synchronized (object) {
                    object.notifyAll();
                     c2.setNumber(number);
                    number++;
                    try {
                        c2.run();
                        if(number<=10) {
                            object.wait();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }


                }
            }
        });

        Thread t2 = new Thread(()->{


            t1.start();
            t3.start();
            try {
                t1.join();
                t3.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Simulation ends");

        });

        t2.start();


    }
}

class Service extends Thread{

    String name;
    int number;
    public Service(){

    }
    public Service(String name,int number){
        this.name=name;
        this.number=number;
    }

    public  void send() throws InterruptedException {
        Thread.sleep(1);
        System.out.println("Server pong " +number+" to "+name);

     }
}

class Client extends Thread{

    String name;
    Service s;

    public Client(String name){
       this.name=name;

   }

      @Override
    public  void run() {
        int number=1;
        while (number<11) {
                try {
                    Thread.sleep(100);
                    s = new Service(name, number);
                    System.out.println(name + " ping " + number);
                    s.send();
                    number++;

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

        }
    }
}

class Client1{
    Service1 s ;
    String name;

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    int number;
    public Client1(String name,int number){
        this.name=name;
        this.number=number;
    }
    public Client1(){

    }
    public synchronized void run() throws InterruptedException {
        s= new Service1(name,number);
        System.out.println(name + " ping " + number);
        s.send();

    }

}

class Service1{
    String name;
    int number;
    public Service1(){

    }
    public Service1(String name,int number){
        this.name=name;
        this.number=number;
    }

    public synchronized void send() throws InterruptedException {

        System.out.println("Server pong " +number+" to "+name);

    }
}




